#!/usr/bin/python
# -- coding:utf8 --
import asyncio
import os
import platform
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
import lightgbm as lgb
import pandas as pd
import numpy as np
import re
import datetime
from collections import Counter
import unidecode
import requests
import json
import time
import random
from xingwei.xx_sms_key_20240604 import *
from xingwei.utils import select_sql_mysql_v1

import warnings
# warnings.filterwarnings('ignore')

if platform.system() != "Windows":
    import uvloop
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
else:
    import nest_asyncio
    nest_asyncio.apply()

# 元数据获取
app_del_key = "google|Calculator|Samsung|Microsoft|miui|realme|Xiaomi|com.mi.global|lenovo"
app_del_key = app_del_key.lower()

current_path = os.path.dirname(__file__)
file_path_name1 = './model/ph-app-贷款类码表-维护-20240617.xls'
file_path_name1 = file_path_name1.replace("./", current_path + "/")
df_gg_loan = pd.read_excel(file_path_name1)

file_path_name2 = './model/PH-短信码表-持续维护更新-20240615.xls'
file_path_name2 = file_path_name2.replace("./", current_path + "/")
df_sms_org = pd.read_excel(file_path_name2)

file_path_name3 = './model/v1_20240619.model'
file_path_name3 = file_path_name3.replace("./", current_path + "/")
lgbm_model = lgb.Booster(model_file=file_path_name3)


print("模型所需本地数据加载成功")

def p2score(p, pdo=40, base_score=700):
    """
    概率转换为分数的函数
    说明：概率p=坏的概率
    :param p:
    :param pdo:
    :param base_score:
    :return:
    """
    b = pdo / np.log(2)
    a = base_score + b * np.log(1 / 20)
    if p < 0:
        return -99999
    min_p = 1e-9
    max_p = 1 - min_p
    if p >= max_p:
        p = max_p
    elif p <= min_p:
        p = min_p
    score = int(a + b * np.log((1 - p) / (p)))
    if score < 300:
        return 300
    elif score > 900:
        return 900
    else:
        return score


def get_url_data(ApplyNO):
    # 获取订单的URL连接和申请时间
    url_sql = """
    select
    tab1.ApplyNO
    ,tab1.FileUrl
    ,tab2.ApplyDate
    ,tab2.UserId
    from 
    (
    select 
    ApplyNO
    ,FileUrl
    from BindingFileUrl
    where ApplyNO = '{}'
    ) as tab1
    left join SysJkApply as tab2
    on tab1.ApplyNO = tab2.ApplyNO
    """.format(ApplyNO)

    df = select_sql_mysql_v1(url_sql)
    return df


def get_basic_data(UserId):
    # 获取用户基本信息的数据
    basic_sql = """
    select 
    t2.UserID as UserId
    ,t1.Maritalstatus
    ,t1.Sex
    ,t2.Birthday
    from 
    (
        select distinct
        tab2.MemberId
        ,tab2.Maritalstatus
        ,tab2.Sex
        from 
        (
        select 
        MemberId
        ,max(CreatDate) as CreatDate
        from SysBasicInformation
        where MemberID = "{}"
        group by MemberId
        ) as tab1 
        inner join SysBasicInformation as tab2 
        on tab1.MemberId = tab2.MemberId and tab1.CreatDate = tab2.CreatDate
    ) as t1
    left join
    (
        select distinct
        tab2.UserID
        ,tab2.Birthday
        from 
        (
        select 
        UserID
        ,max(CreatDate) as CreatDate
        from SysCardAuthentication
        where UserID = "{}"
        group by UserID
        ) as tab1 
        inner join SysCardAuthentication as tab2
        on tab1.UserID = tab2.UserID and tab1.CreatDate = tab2.CreatDate 
    ) as t2
    on t1.MemberId = t2.UserID

    """.format(UserId, UserId)

    df = select_sql_mysql_v1(basic_sql)
    return df


def cal_strdate_to_int(apply_time):  # 13位时间戳
    s_t = time.strptime(str(apply_time), "%Y-%m-%d %H:%M:%S")
    return int(time.mktime(s_t)) * 1000


def cal_strdate_to_datetime(apply_time):
    s_t = time.strptime(str(apply_time), "%Y-%m-%d %H:%M:%S")
    s_t = int(time.mktime(s_t)) * 1000
    date_time = datetime.datetime.fromtimestamp(s_t / 1000)
    return date_time


def tmp_d(fz, fm):
    if fm == 0 or not fm:
        return -999
    else:
        return round(fz / fm, 4)


def get_new_score_params(ApplyNO):
    url_df = get_url_data(ApplyNO)
    basic_df = get_basic_data(url_df['UserId'][0])
    df = url_df.merge(basic_df)

    order_id = df['ApplyNO'][0]
    apply_time = df['ApplyDate'][0]
    url = df['FileUrl'][0]
    apply_time = str(apply_time)
    
    # 获取app
    response = requests.get(url)
    data = response.json()
    data = json.loads(data)
    app_json = data["apps"]
    app_data = json.loads(app_json)
    # 获取sms
    sms_json = data["smss"]
    sms_json = json.loads(sms_json)

    """ 1基本信息模块 """

    y = df["Birthday"][0]
    x = df["ApplyDate"][0]
    marriage = df["Maritalstatus"][0]
    sex = df["Sex"][0]
    
    return {
        'order_id': order_id,
        'apply_time': apply_time,
        'app_data': app_data,
        'sms_json': sms_json,
        'y': y,
        'x': x,
        'marriage': marriage,
        'sex': sex,
    }
    
def test_get_new_score(ApplyNO):
    params = get_new_score_params(ApplyNO)

    # params = convert_to_str(params)
    # json_data = json.dumps(params)
    # path = current_path + "/model/score_test_params.json"
    # with open(path, "w") as f:
    #     json.dump(json_data, f, indent=4)
    # print(json_data)
    
    res = get_new_score(params)

    return res

def create_test_params(ApplyNO):
    # 根据订单查询数据
    params = get_new_score_params(ApplyNO)

    params = convert_to_str(params)
    json_data = json.dumps(params)
    os.makedirs("./model", exist_ok=True)
    path = current_path + "/model/score_test_params.json"
    with open(path, "w") as f:
        json.dump(json_data, f, indent=4)
    print(json_data)
    print("创建测试数据成功！")
    
    return json_data

def convert_to_str(obj):
    """
    如果遇到 object 就转换为字符串
    """
    if isinstance(obj, dict):
        return {k: convert_to_str(v) for k, v in obj.items()}
    elif isinstance(obj, list):
        return [convert_to_str(item) for item in obj]
    elif isinstance(obj, object):
        return str(obj)
    else:
        return obj

def get_new_score(params):

    # 订单
    order_id = params['order_id']
    apply_time = params['apply_time']
    apply_time_int = cal_strdate_to_int(str(apply_time))
    apply_time_datetime = cal_strdate_to_datetime(apply_time)
    # y = Birthday
    y = params['y']
    # x = ApplyDate
    x = params['x']
    # 计算年龄
    y = str(y)[:4]
    x = str(x)[:4]
    age = int(x) - int(y)
    # 婚姻状态
    marriage = params['marriage']
    marriage = int(marriage)
    # 性别
    sex = params['sex']
    sex = int(sex)
    # app list dict
    app_data = params['app_data']
    # sms list dict
    sms_json = params['sms_json']


    """ 2 app模块 """

    app_data = [x for x in app_data if x['appName'] and x['packageName']
                and x['is_system'] == "0" and x['firstInstallTime'] >= "2020-01-01" and x[
                    'lastUpdateTime'] >= "2020-01-01"]

    n1 = len(app_data)
    app_data = [x for x in app_data if not (x['firstInstallTime'] == x['lastUpdateTime']
                                            and (apply_time_int - cal_strdate_to_int(
                x['firstInstallTime'])) / 3600 / 1000 / 24 / 365 >= 1)]
    app_data = [x for x in app_data if
                not re.search(app_del_key.lower(), x['appName'].lower() + x['packageName'].lower())]

    first_install_time = [cal_strdate_to_int(x['firstInstallTime']) for x in app_data]
    last_update_time = [cal_strdate_to_int(x['lastUpdateTime']) for x in app_data]

    in_hours = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in first_install_time]
    up_hours = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in last_update_time]
    all_hours = [(y - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x, y in
                 zip(first_install_time, last_update_time)]

    in_hours_int = [int(x) for x in in_hours]

    """ 计算inday """
    app_name_and_id = [x['appName'] + ' ' + x['packageName'] + "@ xw " + str(
        (apply_time_int - cal_strdate_to_int(x['firstInstallTime'])) / 3600 / 1000 / 24 + random.random() * 0.0001) for
                       x in
                       app_data]
    app_1d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 1]
    app_3d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 3]
    app_7d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 7]
    app_15d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 15]
    app_30d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 30]
    app_60d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 60]
    app_90d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 90]
    app_180d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 180]
    app_360d = [app_name_and_id[in_hours.index(x)] for x in in_hours if x <= 360]

    app_name_and_id_gg = [x['packageName'] for x in app_data]
    app_1d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 1]
    app_3d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 3]
    app_7d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 7]
    app_15d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 15]
    app_30d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 30]
    app_60d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 60]
    app_90d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 90]
    app_180d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 180]
    app_360d_gg = [app_name_and_id_gg[in_hours.index(x)] for x in in_hours if x <= 360]

    app_cnt = len(app_name_and_id)
    app_1d_cnt = len(app_1d)
    app_3d_cnt = len(app_3d)
    app_7d_cnt = len(app_7d)
    app_15d_cnt = len(app_15d)
    app_30d_cnt = len(app_30d)
    app_60d_cnt = len(app_60d)
    app_90d_cnt = len(app_90d)
    app_180d_cnt = len(app_180d)
    app_360d_cnt = len(app_360d)

    df_gg_loan.shape
    package_id_2m = df_gg_loan[df_gg_loan['installnum'] >= 200 * 10000]["package_id"]
    package_id_2m = list(package_id_2m)
    package_id_2m = '@|'.join(package_id_2m)

    package_id_1m = df_gg_loan[df_gg_loan['installnum'] >= 100 * 10000]["package_id"]
    package_id_1m = list(package_id_1m)
    package_id_1m = '@|'.join(package_id_1m)

    package_id_50w = df_gg_loan[df_gg_loan['installnum'] >= 50 * 10000]["package_id"]
    package_id_50w = list(package_id_50w)
    package_id_50w = '@|'.join(package_id_50w)

    package_id_loan = df_gg_loan[df_gg_loan['installnum'] >= 0 * 10000]["package_id"]
    package_id_loan = list(package_id_loan)
    package_id_loan = '@|'.join(package_id_loan)

    package_id_48 = df_gg_loan[df_gg_loan['score'] >= 4.8]["package_id"]
    package_id_48 = list(package_id_48)
    package_id_48 = '@|'.join(package_id_48)

    package_id_46 = df_gg_loan[df_gg_loan['score'] >= 4.6]["package_id"]
    package_id_46 = list(package_id_46)
    package_id_46 = '@|'.join(package_id_46)

    package_id_44 = df_gg_loan[df_gg_loan['score'] >= 4.4]["package_id"]
    package_id_44 = list(package_id_44)
    package_id_44 = '@|'.join(package_id_44)

    package_id_42 = df_gg_loan[df_gg_loan['score'] >= 4.2]["package_id"]
    package_id_42 = list(package_id_42)

    package_id_42 = '@|'.join(package_id_42)

    app_cash_3d = [x for x in app_3d if re.search(package_id_loan, x)]
    app_cash_3d_cnt = len(app_cash_3d)
    app_cash_360d = [x for x in app_360d if re.search(package_id_loan, x)]
    app_cash_360d_cnt = len(app_cash_360d)
    app_cash_3d_div_360d_inday = tmp_d(app_cash_3d_cnt, app_cash_360d_cnt)

    app_cash_7d = [x for x in app_7d if re.search(package_id_loan, x)]
    app_cash_7d_cnt = len(app_cash_7d)
    app_cash_7d_rate_inday = tmp_d(app_cash_7d_cnt, app_7d_cnt)

    app_cash_46_360d = [x for x in app_360d_gg if re.search(package_id_46, x + "@")]
    app_cash_46_360d_cnt = len(app_cash_46_360d)
    app_cash_46_15d = [x for x in app_15d_gg if re.search(package_id_46, x + "@")]
    app_cash_46_15d_cnt = len(app_cash_46_15d)
    app_cash_15d_div_360d_46_inday = tmp_d(app_cash_46_15d_cnt, app_cash_46_360d_cnt)

    app_cash_42_360d = [x for x in app_360d_gg if re.search(package_id_42, x + "@")]
    app_cash_42_360d_cnt = len(app_cash_42_360d)
    app_cash_42_15d = [x for x in app_15d_gg if re.search(package_id_42, x + "@")]
    app_cash_42_15d_cnt = len(app_cash_42_15d)
    app_cash_15d_div_360d_42_inday = tmp_d(app_cash_42_15d_cnt, app_cash_42_360d_cnt)

    app_cash_15d = [x for x in app_15d if re.search(package_id_loan, x)]
    app_cash_15d_cnt = len(app_cash_15d)
    app_cash_3d_div_15d_inday = tmp_d(app_cash_3d_cnt, app_cash_15d_cnt)

    app_cash_48_7d = [x for x in app_7d_gg if re.search(package_id_48, x + "@")]
    app_cash_48_7d_cnt = len(app_cash_48_7d)
    app_cash_48_60d = [x for x in app_60d_gg if re.search(package_id_48, x + "@")]
    app_cash_48_60d_cnt = len(app_cash_48_60d)
    app_cash_7d_div_60d_48_inday = tmp_d(app_cash_48_7d_cnt, app_cash_48_60d_cnt)

    app_cash_30d = [x for x in app_30d if re.search(package_id_loan, x)]
    app_cash_30d_cnt = len(app_cash_30d)
    app_cash_3d_div_30d_inday = tmp_d(app_cash_3d_cnt, app_cash_30d_cnt)

    app_cash_1d = [x for x in app_1d if re.search(package_id_loan, x)]
    app_cash_1d_cnt = len(app_cash_1d)
    app_cash_1d_div_3d_inday = tmp_d(app_cash_1d_cnt, app_cash_3d_cnt)

    """ 计算 upday """
    app_name_and_id = [x['appName'] + ' ' + x['packageName'] + "@ xw " + str(
        (apply_time_int - cal_strdate_to_int(x['lastUpdateTime'])) / 3600 / 1000 / 24 + random.random() * 0.0001) for x
                       in
                       app_data]
    app_1d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 1]
    app_3d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 3]
    app_7d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 7]
    app_15d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 15]
    app_30d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 30]
    app_60d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 60]
    app_90d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 90]
    app_180d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 180]
    app_360d = [app_name_and_id[up_hours.index(x)] for x in up_hours if x <= 360]

    app_name_and_id_gg = [x['packageName'] for x in app_data]
    app_1d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 1]
    app_3d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 3]
    app_7d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 7]
    app_15d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 15]
    app_30d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 30]
    app_60d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 60]
    app_90d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 90]
    app_180d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 180]
    app_360d_gg = [app_name_and_id_gg[up_hours.index(x)] for x in up_hours if x <= 360]

    app_cash_50w_1d = [x for x in app_1d_gg if re.search(package_id_50w, x + "@")]
    app_cash_50w_1d_cnt = len(app_cash_50w_1d)
    app_cash_50w_3d = [x for x in app_3d_gg if re.search(package_id_50w, x + "@")]
    app_cash_50w_3d_cnt = len(app_cash_50w_3d)
    app_cash_1d_div_3d_50w_upday = tmp_d(app_cash_50w_1d_cnt, app_cash_50w_3d_cnt)

    app_cash_3d = [x for x in app_3d if re.search(package_id_loan, x)]
    app_cash_3d_cnt = len(app_cash_3d)
    app_cash_15d = [x for x in app_15d if re.search(package_id_loan, x)]
    app_cash_15d_cnt = len(app_cash_15d)
    app_cash_3d_div_15d_upday = tmp_d(app_cash_3d_cnt, app_cash_15d_cnt)

    in_hours_int = [int(x) for x in in_hours]
    app_inday_majority_upday = Counter(in_hours_int).most_common(1)[0][0]

    """ 计算 tfidf """
    sms_json = [x for x in sms_json if "sms_content" in x.keys()]
    sms_json = [x for x in sms_json if x['sms_content'] and x['send_time'] and x['contactor_name']]
    sms_json = [x for x in sms_json if x['send_time'] >= '2019-10-10 00:00:00']  # 42959038-03-26 03:03:43
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['sms_content'].lower()))]
    sms_json = [x for x in sms_json if not re.search(sms_dele_key, unidecode.unidecode(x['contactor_name'].lower()))]

    body = [item['sms_content'].replace('\n', '').lower() + " " + item['contactor_name'].replace('\n', '').lower() for
            item
            in sms_json]

    def cal_wb(x):
        x = str(x)
        x = x.lower()
        x = re.sub("lo@n", "loan", x)
        x = re.sub("ioan", "loan", x)
        x = re.sub("ioan", "loan", x)
        x = re.sub("l0an", "loan", x)
        x = re.sub("udpate", "update", x)
        x = re.sub("verlflcatlon", "verification", x)
        x = re.sub("l\*an", "loan", x)
        x = x.replace('\n', ' ')
        x = re.sub(r'[^a-zA-Z0-9 %]', '', x)
        x = re.sub(' {2,}', ' ', x)  # 空格>=2 替换成1个
        x = x.lstrip()
        return x.lower()

    body = [cal_wb(unidecode.unidecode(x.lower())) for x in body]  # 去掉重音符
    send_time = [cal_strdate_to_int(item['send_time']) for item in sms_json]
    day = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in send_time]

    body_1d = [body[day.index(x)] for x in day if x <= 1]
    body_3d = [body[day.index(x)] for x in day if x <= 3]
    body_7d = [body[day.index(x)] for x in day if x <= 7]
    body_15d = [body[day.index(x)] for x in day if x <= 15]
    body_30d = [body[day.index(x)] for x in day if x <= 30]
    body_60d = [body[day.index(x)] for x in day if x <= 60]
    body_90d = [body[day.index(x)] for x in day if x <= 90]
    body_360d = [body[day.index(x)] for x in day if x <= 360]
    body_cnt = len(body)
    body_1d_cnt = len(body_1d)
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    body_3d_cnt = len(body_3d)
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    body_7d_cnt = len(body_7d)
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    body_15d_cnt = len(body_15d)
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    body_30d_cnt = len(body_30d)
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    body_60d_cnt = len(body_60d)
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    body_90d_cnt = len(body_90d)
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    body_360d_cnt = len(body_360d)
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_7d_reloan = [x for x in body_7d if re.search("reloan| re loan ", x.lower())]
    body_7d_reloan_cnt = len(body_7d_reloan)
    body_90d_reloan = [x for x in body_90d if re.search("reloan| re loan ", x.lower())]
    body_90d_reloan_cnt = len(body_90d_reloan)
    body_reloan_7d_div_90d_tfidf = tmp_d(body_7d_reloan_cnt, body_90d_reloan_cnt)

    body_90d_apply = [x for x in body_90d if re.search("application|apply", x.lower())]
    body_90d_apply_cnt_tfidf = len(body_90d_apply)

    body_1d_pastdue = [x for x in body_1d if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_1d_pastdue_cnt = len(body_1d_pastdue)
    body_15d_pastdue = [x for x in body_15d if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_15d_pastdue_cnt = len(body_15d_pastdue)
    body_pastdue_1d_div_15d_tfidf = tmp_d(body_1d_pastdue_cnt, body_15d_pastdue_cnt)

    body_7d_apply = [x for x in body_7d if re.search("application|apply", x.lower())]
    body_7d_apply_cnt = len(body_7d_apply)
    body_30d_apply = [x for x in body_30d if re.search("application|apply", x.lower())]
    body_30d_apply_cnt = len(body_30d_apply)
    body_apply_7d_div_30d_tfidf = tmp_d(body_7d_apply_cnt, body_30d_apply_cnt)

    body_3d_now = [x for x in body_3d if re.search("today|now|tomorrow", x.lower())]
    body_3d_now_cnt = len(body_3d_now)
    body_7d_now = [x for x in body_7d if re.search("today|now|tomorrow", x.lower())]
    body_7d_now_cnt = len(body_7d_now)
    body_now_3d_div_7d_tfidf = tmp_d(body_3d_now_cnt, body_7d_now_cnt)

    body_3d_apply = [x for x in body_3d if re.search("application|apply", x.lower())]
    body_3d_apply_cnt = len(body_3d_apply)
    body_apply_3d_div_7d_tfidf = tmp_d(body_3d_apply_cnt, body_7d_apply_cnt)

    body_15d_advance = [x for x in body_15d if re.search("advance", x.lower())]
    body_15d_advance_cnt = len(body_15d_advance)
    body_advance = [x for x in body if re.search("advance", x.lower())]
    body_advance_cnt = len(body_advance)
    body_15d_advance_div_advance_tfidf = tmp_d(body_15d_advance_cnt, body_advance_cnt)

    body_60d_reference = [x for x in body_60d if re.search("reference", x.lower())]
    body_60d_reference_cnt = len(body_60d_reference)
    body_360d_reference = [x for x in body_360d if re.search("reference", x.lower())]
    body_360d_reference_cnt = len(body_360d_reference)
    body_reference_60d_div_360d_tfidf = tmp_d(body_60d_reference_cnt, body_360d_reference_cnt)

    body_7d_legal = [x for x in body_7d if re.search(' legal | law | atty ', x.lower())]
    body_7d_legal_cnt = len(body_7d_legal)
    body_7d_legal_rate = tmp_d(body_7d_legal_cnt, body_7d_cnt)
    body_legal = [x for x in body if re.search(' legal | law | atty ', x.lower())]
    body_legal_cnt = len(body_legal)
    body_7d_legal_div_legal_tfidf = tmp_d(body_7d_legal_cnt, body_legal_cnt)

    body_15d_https = [x for x in body_15d if re.search("https", x.lower())]
    body_15d_https_cnt = len(body_15d_https)
    body_360d_https = [x for x in body_360d if re.search("https", x.lower())]
    body_360d_https_cnt = len(body_360d_https)
    body_https_15d_div_360d_tfidf = tmp_d(body_15d_https_cnt, body_360d_https_cnt)

    body_1d_legal = [x for x in body_1d if re.search(' legal | law | atty ', x.lower())]
    body_1d_legal_cnt = len(body_1d_legal)
    body_15d_legal = [x for x in body_15d if re.search(' legal | law | atty ', x.lower())]
    body_15d_legal_cnt = len(body_15d_legal)
    body_legal_1d_div_15d_tfidf = tmp_d(body_1d_legal_cnt, body_15d_legal_cnt)

    body_7d_success = [x for x in body_7d if re.search("success", x.lower())]
    body_7d_success_cnt = len(body_7d_success)
    body_30d_success = [x for x in body_30d if re.search("success", x.lower())]
    body_30d_success_cnt = len(body_30d_success)
    body_success_7d_div_30d_tfidf = tmp_d(body_7d_success_cnt, body_30d_success_cnt)

    body_15d_utang = [x for x in body_15d if re.search("unsettle|utang|debtor|unpaid", x.lower())]
    body_15d_utang_cnt = len(body_15d_utang)
    body_360d_utang = [x for x in body_360d if re.search("unsettle|utang|debtor|unpaid", x.lower())]
    body_360d_utang_cnt = len(body_360d_utang)
    body_utang_15d_div_360d_tfidf = tmp_d(body_15d_utang_cnt, body_360d_utang_cnt)

    body_1d_paid = [x for x in body_1d if re.search(" paid | received | posted | settled ", x.lower())]
    body_1d_paid_cnt = len(body_1d_paid)
    body_3d_paid = [x for x in body_3d if re.search(" paid | received | posted | settled ", x.lower())]
    body_3d_paid_cnt = len(body_3d_paid)
    body_paid_1d_div_3d_tfidf = tmp_d(body_1d_paid_cnt, body_3d_paid_cnt)

    body_7d_collection = [x for x in body_7d if re.search("collection", x.lower())]
    body_7d_collection_cnt = len(body_7d_collection)
    body_90d_collection = [x for x in body_90d if re.search("collection", x.lower())]
    body_90d_collection_cnt = len(body_90d_collection)
    body_collection_7d_div_90d_tfidf = tmp_d(body_7d_collection_cnt, body_90d_collection_cnt)

    body_90d_bill = [x for x in body_90d if re.search(" balance | bill | balanse |statement", x.lower())]
    body_90d_bill_cnt = len(body_90d_bill)
    body_90d_bill_rate = tmp_d(body_90d_bill_cnt, body_90d_cnt)
    body_bill = [x for x in body if re.search(" balance | bill | balanse |statement", x.lower())]
    body_bill_cnt = len(body_bill)
    body_90d_bill_div_bill_tfidf = tmp_d(body_90d_bill_cnt, body_bill_cnt)

    body_7d_pastdue = [x for x in body_7d if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_7d_pastdue_cnt = len(body_7d_pastdue)
    body_7d_pastdue_rate = tmp_d(body_7d_pastdue_cnt, body_7d_cnt)
    body_pastdue = [x for x in body if re.search('past due|pastdue|over due|overdue', x.lower())]
    body_pastdue_cnt = len(body_pastdue)
    body_7d_pastdue_div_pastdue_tfidf = tmp_d(body_7d_pastdue_cnt, body_pastdue_cnt)

    body_360d_now = [x for x in body_360d if re.search("today|now|tomorrow", x.lower())]
    body_360d_now_cnt = len(body_360d_now)
    body_now_3d_div_360d_tfidf = tmp_d(body_3d_now_cnt, body_360d_now_cnt)

    body_1d_withdraw = [x for x in body_1d if re.search("withdraw", x.lower())]
    body_1d_withdraw_cnt = len(body_1d_withdraw)
    body_7d_withdraw = [x for x in body_7d if re.search("withdraw", x.lower())]
    body_7d_withdraw_cnt = len(body_7d_withdraw)
    body_withdraw_1d_div_7d_tfidf = tmp_d(body_1d_withdraw_cnt, body_7d_withdraw_cnt)

    body_30d_https = [x for x in body_30d if re.search("https", x.lower())]
    body_30d_https_cnt = len(body_30d_https)
    body_https_15d_div_30d_tfidf = tmp_d(body_15d_https_cnt, body_30d_https_cnt)

    body_1d_now = [x for x in body_1d if re.search("today|now|tomorrow", x.lower())]
    body_1d_now_cnt = len(body_1d_now)
    body_now_1d_div_3d_tfidf = tmp_d(body_1d_now_cnt, body_3d_now_cnt)

    """ 计算 org """
    sms_topcash_key = 'mabiliscash|creditpeso|homecredit|tala|mocamoca|gopeso|digido|juanhand|pesoloan|onlineloans|okpes|lpeso|pesocash|fastcash|cashalo'
    sms_1m_cash_key = 'valleyloan|fastcash|maypera|wowpera|lemonloan|pesohere|zippeso|pesoredee|easypeso|mrcash|peramoo|cashme|moneycat|upeso|mocasa|cashbee|mabiliscash|creditpeso|homecredit|tala|mocamoca|gopeso|digido|juanhand|pesoloan|onlineloans|okpes|lpeso|pesocash|fastcash|cashalo'

    msm_org = list(df_sms_org["msm_org"])
    len(msm_org)
    sms_org_pattern = "|".join(str(msm_org))
    sms_org_pattern

    # 新逻辑
    def cal_tq_org(x):
        x = str(x)
        x = x.strip()
        x = unidecode.unidecode(x.lower())
        pattern = "$(.*?)$|<(.*?)>|【(.*?)】|\[(.*?)\]|\((.*?)\)|\{(.*?)\}|（(.*?)）|(^[^0-9]{0,15}):|(^[^0-9]{0,15})："
        matches = re.search(pattern, x)
        # matches = re.match(pattern, x)
        y = "-999"
        if matches:
            s = matches.start()
            e = matches.end()
            y = x[s:e]
            y = re.sub(r'[^a-zA-Z0-9áéíóúüÁÉÍÓÚÜ]', '', y)  # 保留数字英文和空格
        if len(y) >= 16:
            y = "-999"
        return y

    def cal_tq_org_02(x):
        x = str(x).lower()
        x = re.sub(r'[^a-zA-Z0-9áéíóúüÁÉÍÓÚÜ@]', '', x)
        pattern = sms_org_pattern
        matches = re.search(pattern, x)
        # matches = re.match(pattern, x)
        y = "-999"
        if matches:
            s = matches.start()
            e = matches.end()
            y = x[s:e]
        if len(y) >= 16:
            y = "-999"
        return y

    def cal_sms_content(x):
        x = str(x)
        x = x.lower()
        x = re.sub("lo@n", "loan", x)
        x = re.sub("ioan", "loan", x)
        x = re.sub("ioan", "loan", x)
        x = re.sub("l0an", "loan", x)
        x = re.sub("udpate", "update", x)
        x = re.sub("verlflcatlon", "verification", x)
        x = re.sub("l\*an", "loan", x)
        x = x.replace('\n', ' ')
        x = re.sub(r'[^a-zA-Z0-9 %]', '', x)
        x = re.sub(' {2,}', ' ', x)  # 空格>=2 替换成1个
        x = x.lstrip()
        return x.lower()

    def cal_boy_type(x, type):
        y = 0
        if re.search(type, x):
            y = 1
        return y

    sms_json = [{**x, 'sms_org': cal_tq_org(x["sms_content"])} for x in sms_json]
    sms_json = [{**x, "sms_org_ok": cal_tq_org_02(x["sms_org"] + x["contactor_name"])} for x in sms_json]
    sms_json = [{**x, "sms_content_ok": cal_sms_content(x["sms_content"])} for x in sms_json]
    sms_json = [{**x, "sms_verif_key": cal_boy_type(x["sms_content_ok"], sms_verif_key)} for x in sms_json]
    sms_json = [{**x, "sms_cash_key": cal_boy_type(x["sms_content_ok"], sms_cash_key)} for x in sms_json]
    sms_json = [{**x, "sms_loan_key": cal_boy_type(x["sms_content_ok"], sms_loan_key)} for x in sms_json]
    sms_json = [{**x, "sms_repayment_key": cal_boy_type(x["sms_content_ok"], sms_repayment_key)} for x in sms_json]
    sms_json = [{**x, "sms_expire_key": cal_boy_type(x["sms_content_ok"], sms_expire_key)} for x in sms_json]
    sms_json = [{**x, "sms_bill_key": cal_boy_type(x["sms_content_ok"], sms_bill_key)} for x in sms_json]
    sms_json = [{**x, "sms_overdue_serious_key": cal_boy_type(x["sms_content_ok"], sms_overdue_serious_key)} for x in
                sms_json]
    sms_json = [
        {**x, "sms_overdue_key": cal_boy_type(x["sms_content_ok"], sms_overdue_key + "|" + sms_overdue_serious_key)}
        for x in sms_json]
    sms_json = [{**x, "sms_topcash_key": cal_boy_type(x["sms_org_ok"], sms_topcash_key)} for x in sms_json]
    sms_json = [{**x, "sms_1m_cash_key": cal_boy_type(x["sms_org_ok"], sms_1m_cash_key)} for x in sms_json]

    sms_json = [{**x, "day": (apply_time_int - cal_strdate_to_int(x["send_time"])) / 3600 / 1000 / 24} for x in
                sms_json]

    body = [x for x in sms_json]
    body_1d = [x for x in sms_json if x["day"] <= 1]
    body_3d = [x for x in sms_json if x["day"] <= 3]
    body_7d = [x for x in sms_json if x["day"] <= 7]
    body_15d = [x for x in sms_json if x["day"] <= 15]
    body_30d = [x for x in sms_json if x["day"] <= 30]
    body_60d = [x for x in sms_json if x["day"] <= 60]
    body_90d = [x for x in sms_json if x["day"] <= 90]
    body_360d = [x for x in sms_json if x["day"] <= 360]

    body_cnt = len(body)
    body_1d_cnt = len(body_1d)
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    body_3d_cnt = len(body_3d)
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    body_7d_cnt = len(body_7d)
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    body_15d_cnt = len(body_15d)
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    body_30d_cnt = len(body_30d)
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    body_60d_cnt = len(body_60d)
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    body_90d_cnt = len(body_90d)
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    body_360d_cnt = len(body_360d)
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_15d_verif = [x['sms_org_ok'] for x in sms_json if
                      x["sms_verif_key"] == 1 and x["day"] <= 15 and x['sms_org_ok'] != "-999"]
    body_15d_verif = set(body_15d_verif)
    body_15d_verif_cnt = len(body_15d_verif)
    body_15d_verif_rate_org = tmp_d(body_15d_verif_cnt, body_15d_cnt)

    body_7d_expire_1m_cash = [x['sms_org_ok'] for x in sms_json if
                              x["sms_expire_key"] == 1 and x["sms_1m_cash_key"] == 1 and x["day"] <= 7 and x[
                                  'sms_org_ok'] != "-999"]
    body_7d_expire_1m_cash = set(body_7d_expire_1m_cash)
    body_7d_expire_1m_cash_cnt = len(body_7d_expire_1m_cash)
    body_30d_expire_1m_cash = [x['sms_org_ok'] for x in sms_json if
                               x["sms_expire_key"] == 1 and x["sms_1m_cash_key"] == 1 and x["day"] <= 30 and x[
                                   'sms_org_ok'] != "-999"]
    body_30d_expire_1m_cash = set(body_30d_expire_1m_cash)
    body_30d_expire_1m_cash_cnt = len(body_30d_expire_1m_cash)
    body_expire_1m_cash_7d_div_30d_org = tmp_d(body_7d_expire_1m_cash_cnt, body_30d_expire_1m_cash_cnt)

    body_3d_verif = [x['sms_org_ok'] for x in sms_json if
                     x["sms_verif_key"] == 1 and x["day"] <= 3 and x['sms_org_ok'] != "-999"]
    body_3d_verif = set(body_3d_verif)
    body_3d_verif_cnt_org = len(body_3d_verif)

    body_7d_overdue = [x['sms_org_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                       and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 7 and x[
                           'sms_org_ok'] != "-999"]
    body_7d_overdue = set(body_7d_overdue)
    body_7d_overdue_cnt = len(body_7d_overdue)
    body_90d_overdue = [x['sms_org_ok'] for x in sms_json if x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0
                        and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 90 and x[
                            'sms_org_ok'] != "-999"]
    body_90d_overdue = set(body_90d_overdue)
    body_90d_overdue_cnt = len(body_90d_overdue)
    body_overdue_7d_div_90d_org = tmp_d(body_7d_overdue_cnt, body_90d_overdue_cnt)

    body_3d_overdue_1m_cash = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_1m_cash_key'] == 1
                               and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 3 and
                               x['sms_org_ok'] != "-999"]
    body_3d_overdue_1m_cash = set(body_3d_overdue_1m_cash)
    body_3d_overdue_1m_cash_cnt = len(body_3d_overdue_1m_cash)
    body_7d_overdue_1m_cash = [x['sms_org_ok'] for x in sms_json if
                               x["sms_overdue_key"] == 1 and x['sms_loan_key'] == 0 and x['sms_1m_cash_key'] == 1
                               and x['sms_repayment_key'] == 0 and x['sms_expire_key'] == 0 and x["day"] <= 7 and
                               x['sms_org_ok'] != "-999"]
    body_7d_overdue_1m_cash = set(body_7d_overdue_1m_cash)
    body_7d_overdue_1m_cash_cnt = len(body_7d_overdue_1m_cash)
    body_overdue_1m_cash_3d_div_7d_org = tmp_d(body_3d_overdue_1m_cash_cnt, body_7d_overdue_1m_cash_cnt)

    body_15d_verif = [x['sms_org_ok'] for x in sms_json if
                      x["sms_verif_key"] == 1 and x["day"] <= 15 and x['sms_org_ok'] != "-999"]
    body_15d_verif = set(body_15d_verif)
    body_15d_verif_cnt = len(body_15d_verif)
    body_15d_verif_rate = tmp_d(body_15d_verif_cnt, body_15d_cnt)
    body_verif = [x['sms_org_ok'] for x in sms_json if x["sms_verif_key"] == 1 and x['sms_org_ok'] != "-999"]
    body_verif = set(body_verif)
    body_verif_cnt = len(body_verif)
    body_15d_verif_div_verif_org = tmp_d(body_15d_verif_cnt, body_verif_cnt)

    body_7d_loan = [x['sms_org_ok'] for x in sms_json if
                    x["sms_loan_key"] == 1 and x["day"] <= 7 and x['sms_org_ok'] != "-999"]
    body_7d_loan = set(body_7d_loan)
    body_7d_loan_cnt = len(body_7d_loan)
    body_90d_loan = [x['sms_org_ok'] for x in sms_json if
                     x["sms_loan_key"] == 1 and x["day"] <= 90 and x['sms_org_ok'] != "-999"]
    body_90d_loan = set(body_90d_loan)
    body_90d_loan_cnt = len(body_90d_loan)
    body_loan_7d_div_90d_org = tmp_d(body_7d_loan_cnt, body_90d_loan_cnt)

    body_1d_cash_verif = [x['sms_org_ok'] for x in sms_json if
                          x["sms_verif_key"] == 1 and x["sms_cash_key"] == 1 and x["day"] <= 1 and x[
                              'sms_org_ok'] != "-999"]
    body_1d_cash_verif = set(body_1d_cash_verif)
    body_1d_cash_verif_cnt = len(body_1d_cash_verif)
    body_3d_cash_verif = [x['sms_org_ok'] for x in sms_json if
                          x["sms_verif_key"] == 1 and x["sms_cash_key"] == 1 and x["day"] <= 3 and x[
                              'sms_org_ok'] != "-999"]
    body_3d_cash_verif = set(body_3d_cash_verif)
    body_3d_cash_verif_cnt = len(body_3d_cash_verif)
    body_cash_verif_1d_div_3d_org = tmp_d(body_1d_cash_verif_cnt, body_3d_cash_verif_cnt)

    body_1d_loan = [x['sms_org_ok'] for x in sms_json if
                    x["sms_loan_key"] == 1 and x["day"] <= 1 and x['sms_org_ok'] != "-999"]
    body_1d_loan = set(body_1d_loan)
    body_1d_loan_cnt = len(body_1d_loan)
    body_loan_1d_div_7d_org = tmp_d(body_1d_loan_cnt, body_7d_loan_cnt)

    body_3d_bill = [x['sms_org_ok'] for x in sms_json if
                    x["sms_bill_key"] == 1 and x["day"] <= 3 and x['sms_org_ok'] != "-999"]
    body_3d_bill = set(body_3d_bill)
    body_3d_bill_cnt = len(body_3d_bill)
    body_90d_bill = [x['sms_org_ok'] for x in sms_json if
                     x["sms_bill_key"] == 1 and x["day"] <= 90 and x['sms_org_ok'] != "-999"]
    body_90d_bill = set(body_90d_bill)
    body_90d_bill_cnt = len(body_90d_bill)
    body_bill_3d_div_90d_org = tmp_d(body_3d_bill_cnt, body_90d_bill_cnt)

    """ 计算 base sms"""
    sms_json = [x for x in sms_json if "sms_content" in x.keys()]
    sms_json = [x for x in sms_json if x['sms_content'] and x['send_time'] and x['contactor_name']]
    sms_json = [x for x in sms_json if x['send_time'] >= '2019-10-10 00:00:00']  # 42959038-03-26 03:03:43
    sms_json = [x for x in sms_json if
                not re.search(sms_dele_key, unidecode.unidecode(x['sms_content'].lower()))]  # 42959038-03-26 03:03:43
    sms_json = [x for x in sms_json if
                not re.search(sms_dele_key,
                              unidecode.unidecode(x['contactor_name'].lower()))]  # 42959038-03-26 03:03:43

    body = [item['sms_content'].replace('\n', '').lower() + " " + item['contactor_name'].replace('\n', '').lower() for
            item
            in sms_json]  # 删除换行符
    body = [cal_wb(unidecode.unidecode(x.lower())) for x in body]  # 去掉重音符

    send_time = [cal_strdate_to_int(item['send_time']) for item in sms_json]
    day = [(apply_time_int - x) / 3600 / 1000 / 24 + random.random() * 0.0001 for x in send_time]

    body_1d = [body[day.index(x)] for x in day if x <= 1]
    body_3d = [body[day.index(x)] for x in day if x <= 3]
    body_7d = [body[day.index(x)] for x in day if x <= 7]
    body_15d = [body[day.index(x)] for x in day if x <= 15 * 1]
    body_30d = [body[day.index(x)] for x in day if x <= 30 * 1]
    body_60d = [body[day.index(x)] for x in day if x <= 60 * 1]
    body_90d = [body[day.index(x)] for x in day if x <= 90 * 1]
    body_360d = [body[day.index(x)] for x in day if x <= 360 * 1]

    body_cnt = len(body)
    body_1d_cnt = len(body_1d)
    body_1d_rate = tmp_d(body_1d_cnt, body_cnt)
    body_3d_cnt = len(body_3d)
    body_3d_rate = tmp_d(body_3d_cnt, body_cnt)
    body_7d_cnt = len(body_7d)
    body_7d_rate = tmp_d(body_7d_cnt, body_cnt)
    body_15d_cnt = len(body_15d)
    body_15d_rate = tmp_d(body_15d_cnt, body_cnt)
    body_30d_cnt = len(body_30d)
    body_30d_rate = tmp_d(body_30d_cnt, body_cnt)
    body_60d_cnt = len(body_60d)
    body_60d_rate = tmp_d(body_60d_cnt, body_cnt)
    body_90d_cnt = len(body_90d)
    body_90d_rate = tmp_d(body_90d_cnt, body_cnt)
    body_360d_cnt = len(body_360d)
    body_360d_rate = tmp_d(body_360d_cnt, body_cnt)

    body_3d_overdue = [x for x in body_3d if re.search(sms_overdue_key + "|" + sms_overdue_serious_key, x)
                       and not re.search(sms_loan_key, x)
                       and not re.search(sms_repayment_key, x)
                       and not re.search(sms_expire_key, x)]
    body_3d_overdue_cnt = len(body_3d_overdue)
    body_3d_overdue_rate = tmp_d(body_3d_overdue_cnt, body_3d_cnt)
    body_overdue = [x for x in body if re.search(sms_overdue_key + "|" + sms_overdue_serious_key, x)
                    and not re.search(sms_loan_key, x)
                    and not re.search(sms_repayment_key, x)
                    and not re.search(sms_expire_key, x)]
    body_overdue_cnt = len(body_overdue)
    body_3d_overdue_div_overdue = tmp_d(body_3d_overdue_cnt, body_overdue_cnt)

    body_7d_cash_verif = [x for x in body_7d if
                          re.search(sms_cash_key, x.lower()) and re.search(sms_verif_key, x.lower())]
    body_7d_cash_verif_cnt = len(body_7d_cash_verif)
    body_cash_verif = [x for x in body if re.search(sms_cash_key, x.lower()) and re.search(sms_verif_key, x.lower())]
    body_cash_verif_cnt = len(body_cash_verif)
    body_7d_cash_verif_div_cash_verif = tmp_d(body_7d_cash_verif_cnt, body_cash_verif_cnt)

    body_3d_settle = [x for x in body_3d if re.search(sms_repayment_key, x)]
    body_3d_settle_cnt = len(body_3d_settle)
    body_7d_settle = [x for x in body_7d if re.search(sms_repayment_key, x)]
    body_7d_settle_cnt = len(body_7d_settle)
    body_settle_3d_div_7d = tmp_d(body_3d_settle_cnt, body_7d_settle_cnt)

    body_overdue_serious = [x for x in body if re.search(sms_overdue_serious_key, x)
                            and not re.search(sms_loan_key, x)
                            and not re.search(sms_repayment_key, x)
                            and not re.search(sms_expire_key, x)]
    body_overdue_serious_cnt = len(body_overdue_serious)
    body_overdue_serious_rate = tmp_d(body_overdue_serious_cnt, body_cnt)
    body_1d_overdue_serious = [x for x in body_1d if re.search(sms_overdue_serious_key, x)
                               and not re.search(sms_loan_key, x)
                               and not re.search(sms_repayment_key, x)
                               and not re.search(sms_expire_key, x)]
    body_1d_overdue_serious_cnt = len(body_1d_overdue_serious)
    body_1d_overdue_serious_rate = tmp_d(body_1d_overdue_serious_cnt, body_1d_cnt)
    body_1d_overdue_serious_div_overdue_serious = tmp_d(body_1d_overdue_serious_cnt, body_overdue_serious_cnt)

    body_expire = [x for x in body if re.search(sms_expire_key, x)]
    body_expire_cnt = len(body_expire)
    body_expire_rate = tmp_d(body_expire_cnt, body_cnt)
    body_1d_expire = [x for x in body_1d if re.search(sms_expire_key, x)]
    body_1d_expire_cnt = len(body_1d_expire)
    body_1d_expire_rate = tmp_d(body_1d_expire_cnt, body_1d_cnt)
    body_1d_expire_div_expire = tmp_d(body_1d_expire_cnt, body_expire_cnt)

    body_7d_expire = [x for x in body_7d if re.search(sms_expire_key, x)]
    body_7d_expire_cnt = len(body_7d_expire)
    body_15d_expire = [x for x in body_15d if re.search(sms_expire_key, x)]
    body_15d_expire_cnt = len(body_15d_expire)
    body_expire_7d_div_15d = tmp_d(body_7d_expire_cnt, body_15d_expire_cnt)

    body_1d_overdue = [x for x in body_1d if re.search(sms_overdue_key + "|" + sms_overdue_serious_key, x)
                       and not re.search(sms_loan_key, x)
                       and not re.search(sms_repayment_key, x)
                       and not re.search(sms_expire_key, x)]
    body_1d_overdue_cnt = len(body_1d_overdue)
    body_overdue_1d_div_3d = tmp_d(body_1d_overdue_cnt, body_3d_overdue_cnt)

    body_15d_bill = [x for x in body_15d if re.search(sms_bill_key, x)]
    body_15d_bill_cnt = len(body_15d_bill)
    body_60d_bill = [x for x in body_60d if re.search(sms_bill_key, x)]
    body_60d_bill_cnt = len(body_60d_bill)
    body_bill_15d_div_60d = tmp_d(body_15d_bill_cnt, body_60d_bill_cnt)

    df_tmp = pd.DataFrame({

        "app_cash_3d_div_360d_inday": app_cash_3d_div_360d_inday,
        "body_15d_verif_rate_org": body_15d_verif_rate_org,
        "age": age,
        "body_expire_1m_cash_7d_div_30d_org": body_expire_1m_cash_7d_div_30d_org,
        "marriage": marriage,
        "sex": sex,
        "body_reloan_7d_div_90d_tfidf": body_reloan_7d_div_90d_tfidf,
        "body_3d_verif_cnt_org": body_3d_verif_cnt_org,
        "body_90d_apply_cnt_tfidf": body_90d_apply_cnt_tfidf,
        "body_3d_overdue_div_overdue": body_3d_overdue_div_overdue,
        "body_pastdue_1d_div_15d_tfidf": body_pastdue_1d_div_15d_tfidf,
        "body_apply_7d_div_30d_tfidf": body_apply_7d_div_30d_tfidf,
        "body_overdue_7d_div_90d_org": body_overdue_7d_div_90d_org,
        "body_now_3d_div_7d_tfidf": body_now_3d_div_7d_tfidf,
        "body_7d_cash_verif_div_cash_verif": body_7d_cash_verif_div_cash_verif,
        "body_settle_3d_div_7d": body_settle_3d_div_7d,
        "body_1d_overdue_serious_div_overdue_serious": body_1d_overdue_serious_div_overdue_serious,
        "body_overdue_1m_cash_3d_div_7d_org": body_overdue_1m_cash_3d_div_7d_org,
        "body_apply_3d_div_7d_tfidf": body_apply_3d_div_7d_tfidf,
        "body_15d_advance_div_advance_tfidf": body_15d_advance_div_advance_tfidf,
        "body_1d_expire_div_expire": body_1d_expire_div_expire,
        "body_reference_60d_div_360d_tfidf": body_reference_60d_div_360d_tfidf,
        "body_expire_cnt": body_expire_cnt,
        "body_7d_legal_div_legal_tfidf": body_7d_legal_div_legal_tfidf,
        "app_cash_7d_rate_inday": app_cash_7d_rate_inday,
        "body_expire_7d_div_15d": body_expire_7d_div_15d,
        "app_cash_1d_div_3d_50w_upday": app_cash_1d_div_3d_50w_upday,
        "body_overdue_1d_div_3d": body_overdue_1d_div_3d,
        "body_15d_verif_div_verif_org": body_15d_verif_div_verif_org,
        "body_loan_7d_div_90d_org": body_loan_7d_div_90d_org,
        "body_https_15d_div_360d_tfidf": body_https_15d_div_360d_tfidf,
        "app_cash_3d_div_15d_upday": app_cash_3d_div_15d_upday,
        "body_legal_1d_div_15d_tfidf": body_legal_1d_div_15d_tfidf,
        "app_cash_15d_div_360d_46_inday": app_cash_15d_div_360d_46_inday,
        "body_success_7d_div_30d_tfidf": body_success_7d_div_30d_tfidf,
        "body_utang_15d_div_360d_tfidf": body_utang_15d_div_360d_tfidf,
        "body_cash_verif_1d_div_3d_org": body_cash_verif_1d_div_3d_org,
        "body_loan_1d_div_7d_org": body_loan_1d_div_7d_org,
        "body_paid_1d_div_3d_tfidf": body_paid_1d_div_3d_tfidf,
        "body_bill_15d_div_60d": body_bill_15d_div_60d,
        "body_bill_3d_div_90d_org": body_bill_3d_div_90d_org,
        "body_collection_7d_div_90d_tfidf": body_collection_7d_div_90d_tfidf,
        "app_cash_15d_div_360d_42_inday": app_cash_15d_div_360d_42_inday,
        "body_90d_bill_div_bill_tfidf": body_90d_bill_div_bill_tfidf,
        "body_7d_pastdue_div_pastdue_tfidf": body_7d_pastdue_div_pastdue_tfidf,
        "app_cash_3d_div_15d_inday": app_cash_3d_div_15d_inday,
        "app_cash_7d_div_60d_48_inday": app_cash_7d_div_60d_48_inday,
        "body_now_3d_div_360d_tfidf": body_now_3d_div_360d_tfidf,
        "app_cash_3d_div_30d_inday": app_cash_3d_div_30d_inday,
        "body_withdraw_1d_div_7d_tfidf": body_withdraw_1d_div_7d_tfidf,
        "body_https_15d_div_30d_tfidf": body_https_15d_div_30d_tfidf,
        "app_cash_1d_div_3d_inday": app_cash_1d_div_3d_inday,
        "app_inday_majority_upday": app_inday_majority_upday,
        "body_now_1d_div_3d_tfidf": body_now_1d_div_3d_tfidf,

    }, index=[1])

    var = ['app_cash_3d_div_360d_inday',
           'body_15d_verif_rate_org',
           'age',
           'marriage',
           'body_expire_1m_cash_7d_div_30d_org',
           'sex',
           'body_3d_verif_cnt_org',
           'body_reloan_7d_div_90d_tfidf',
           'body_90d_apply_cnt_tfidf',
           'body_pastdue_1d_div_15d_tfidf',
           'body_apply_7d_div_30d_tfidf',
           'body_3d_overdue_div_overdue',
           'body_now_3d_div_7d_tfidf',
           'body_apply_3d_div_7d_tfidf',
           'body_overdue_7d_div_90d_org',
           'body_settle_3d_div_7d',
           'body_1d_expire_div_expire',
           'body_1d_overdue_serious_div_overdue_serious',
           'body_7d_cash_verif_div_cash_verif',
           'body_expire_cnt',
           'body_7d_legal_div_legal_tfidf',
           'body_overdue_1m_cash_3d_div_7d_org',
           'body_success_7d_div_30d_tfidf',
           'body_reference_60d_div_360d_tfidf',
           'body_https_15d_div_360d_tfidf',
           'app_cash_3d_div_15d_upday',
           'app_inday_majority_upday',
           'body_15d_advance_div_advance_tfidf',
           'app_cash_7d_rate_inday',
           'body_expire_7d_div_15d',
           'body_legal_1d_div_15d_tfidf',
           'app_cash_3d_div_15d_inday',
           'body_utang_15d_div_360d_tfidf',
           'app_cash_1d_div_3d_50w_upday',
           'body_15d_verif_div_verif_org',
           'body_7d_pastdue_div_pastdue_tfidf',
           'body_loan_1d_div_7d_org',
           'body_now_1d_div_3d_tfidf',
           'app_cash_15d_div_360d_46_inday',
           'app_cash_15d_div_360d_42_inday',
           'body_collection_7d_div_90d_tfidf',
           'body_paid_1d_div_3d_tfidf',
           'body_withdraw_1d_div_7d_tfidf',
           'body_bill_15d_div_60d',
           'body_cash_verif_1d_div_3d_org',
           'body_now_3d_div_360d_tfidf',
           'body_overdue_1d_div_3d',
           'body_90d_bill_div_bill_tfidf',
           'app_cash_1d_div_3d_inday',
           'app_cash_7d_div_60d_48_inday',
           'body_https_15d_div_30d_tfidf',
           'body_loan_7d_div_90d_org',
           'app_cash_3d_div_30d_inday',
           'body_bill_3d_div_90d_org']

    pred01 = lgbm_model.predict(df_tmp[var])
    pred01 = pred01[0]

    XWWhitListScoreV1 = p2score(pred01)
    # print(order_id, pred01)

    return {
            'XWWhitListScoreV1': XWWhitListScoreV1
        }


if __name__ == '__main__':
    # 外部风控v1测试
    ApplyNO = '168134606437913172'
    # ApplyNO = '168137485036053376'
    # # 第一步：获取数据
    # url_df = get_url_data(ApplyNO)
    # basic_df = get_basic_data(url_df['UserId'][0])
    # raw_df = url_df.merge(basic_df)
    # # 第二步：打出评分
    # new_score = get_new_score(raw_df)
    
    # 测试用例
    # new_score = test_get_new_score(ApplyNO)

    path = current_path + "/model/score_test_params.json"
    with open(path, "r") as f:
        test_params = json.load(f)
    test_params = json.loads(test_params)
    new_score = get_new_score(test_params)

    
    print(new_score)
